const fs=require('fs');
const ejs=require('ejs');
const inquirer=require('inquirer');

let del = false;

if(process.argv.length === 3){
    del = process.argv[2] === '-d'
}

inquirer.prompt([{
    //这里输入问题相关内容,如：
    type:"input",
    message: "请输入状态名称：",
    name:'name',
    validate: (input) => {
        if (!input) {
            return '名称不能为空';
        }

        // 新建目录
        const dir = '../../src/store/modules/' + input;
        if (fs.existsSync(dir)) {
            return '系统已经存在该名称了，请更换其它名称';
        }

        return true;
    }
}]).then((res) => {
    const name = res.name;

    // 新建目录
    const dir = '../../src/store/modules/' + name;
    fs.mkdirSync(dir);

    // 模板渲染，生成文件
    const fileNames = fs.readdirSync('./views');

    fileNames.forEach(fileName => {
        const filePath = `./views/${fileName}`;
        ejs.renderFile(filePath, { name }, function(err,data){
            if(err){
                console.log(err);
            }else{
                fileName = fileName.replace('ejs', 'js');
                let filePath = dir + '/' + fileName;
                fs.writeFileSync(filePath, data);
                filePath = filePath.substr(6);
                console.log(`${filePath}创建成功\n`);
            }
        });
    });

    fs.readFile('../../src/store/modules/index.js', function(err, data){
        if(err){
            console.log(err);
        }else{
            const lines = data.toString().split(/\r\n|\n|\r/gm);
    
            const importIndex = lines.findIndex((line, index) => {
                line = line.trim();
                const nextLine = lines[index+1];
    
                if(line.startsWith('import') 
                    && line.indexOf('./') > -1 
                    && nextLine === ''){
                    return true;
                }
            });
    
            lines.splice(importIndex + 1, 0, `import ${name} from './${name}';`);
    
            let shapesIndex = 0;
            lines.forEach((line, index) => {
                line = line.trim();
    
                if(line.indexOf('const modules') > -1){
                    while(true){
                        index = index + 1;
                        let nextLine = lines[index].trim();
    
                        if(nextLine.indexOf('}') > -1){
                            shapesIndex = index;
                            break;
                        }
                    }
                    return true;
                }
            });
            lines.splice(shapesIndex, 0, `    ${name},`);

            fs.writeFileSync('../../src/store/modules/index.js', lines.join('\n'));
            console.log(`src/shapes/index.js写入${name}成功\n`);
        }
    });
    
    fs.readFile('../../src/store/preloadedState.js', function(err, data){
        if(err){
            console.log(err);
        }else{
            const lines = data.toString().split(/\r\n|\n|\r/gm);
    
            const index = lines.findIndex(line => {
                line = line.trim();
                if(line.indexOf('});') > -1){
                    return true;
                }
            });
    
            lines.splice(index, 0, `    ${name}:{},`);
            fs.writeFileSync('../../src/store/preloadedState.js', lines.join('\n'));
            console.log(`src/store/preloadedState.js写入${name}:{}成功\n`);
        }
    });
});
